package com.rui1024.dao.impl;

import com.rui1024.dao.UserDao;
import com.rui1024.pojo.Management_user;
import com.rui1024.pojo.User;
import com.rui1024.util.Md5Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public User login(Connection con, User user) throws Exception {
        String sql="select * from userinfo where userName=? and userPasswd=?";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1,user.getUserName());
        pstmt.setString(2, Md5Util.encodePwdByMd5(user.getUserPasswd()));
        System.out.println(Md5Util.encodePwdByMd5(user.getUserPasswd()));
        ResultSet rs = pstmt.executeQuery();
        User currentUser=null;
        while (rs.next()){
            currentUser = new User();
            currentUser.setUserName(rs.getString("userName"));
            currentUser.setUserPasswd(rs.getString("userPasswd"));
        }
        return currentUser;
    }


    public Management_user loginman(Connection con, Management_user user) throws Exception {
        String sql="select * from management_user where management_userName=? and management_userPasswd=?";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1,user.getManagement_userName());
        pstmt.setString(2, Md5Util.encodePwdByMd5(user.getManagement_userPasswd()));
        System.out.println(Md5Util.encodePwdByMd5(user.getManagement_userPasswd()));
        ResultSet rs = pstmt.executeQuery();
        Management_user currentUser_man=null;
        while (rs.next()){
            currentUser_man = new Management_user();
            currentUser_man.setManagement_userName(rs.getString("management_userName"));
            currentUser_man.setManagement_userPasswd(rs.getString("management_userPasswd"));
            currentUser_man.setSrc(rs.getString("src"));
            currentUser_man.setPosition(rs.getString("position"));
            currentUser_man.setTalk(rs.getString("talk"));
            currentUser_man.setUsername(rs.getString("username"));
        }
        return currentUser_man;
    }


    public ArrayList<User> usermanlist(Connection con) throws Exception {

        String sql="select * from userinfo ";
        PreparedStatement pstmt = con.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        ArrayList<User> list = new ArrayList();
        User currentUser_list=null;
        while (rs.next()){
            currentUser_list = new User();
            currentUser_list.setUserName(rs.getString("userName"));
            currentUser_list.setUserPasswd(rs.getString("userPasswd"));
            currentUser_list.setThis(rs.getString("This"));
            currentUser_list.setRole(rs.getString("role"));
            list.add(currentUser_list);
        }
        return list;
    }

    public boolean deleteuser(String name, Connection con) throws Exception {
        String sql = "delete from userinfo where userName=?";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1, name);
        int num = pstmt.executeUpdate();
        if (num > 0) {
            return true;
        }
        return false;
    }
}
